/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.util;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * 
 *
 * @author haolw
 * @time 2016/09/28
 */
public class JsonUtil {
	private static SerializerFeature[] serializerFeatures;
	private static Feature[] features;

	static {
		serializerFeatures = new SerializerFeature[] { SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteNullNumberAsZero, SerializerFeature.DisableCircularReferenceDetect };

		features = new Feature[] {

		};
	}

	/**
	 * 序列化
	 * 
	 * @param obj
	 * @return
	 */
	public static String toJson(Object obj) {
		return JSON.toJSONString(obj, serializerFeatures);
	}

	/**
	 * 反序列化
	 * 
	 * @param content
	 * @param T
	 * @return
	 */
	public static <T> T parse(String content, Class<T> clazz) {
		return JSON.parseObject(content, clazz, features);
	}

	/**
	 * 反序列化to list
	 * 
	 * @param content
	 * @param clazz
	 * @return
	 */
	public static <T> List<T> parseArray(String content, Class<T> clazz) {
		return JSON.parseArray(content, clazz);
	}

	/**
	 * 反序列化to map
	 * 
	 * @param content
	 * @param clazz
	 * @return
	 */
	public static Map<String, Object> parseMap(String content) {
		return JSON.parseObject(content, new TypeReference<Map<String, Object>>() {
		});
	}

}
